import JSONBig from 'json-bigint'
const baseUrl = 'https://api.daccf.com';
import { adminId,userToken } from 'utils/config.js'; 
export function request(options) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + options.url,
			data: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"Authorization": uni.getStorageSync('userInfo').accessToken || "",
				"Content-Type": "application/x-www-form-urlencoded",
				"ADMIN_ID": adminId,
				"USER_SERVICE_TOKEN":userToken,
				"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
			},
			dataType: 'string',
			success(res) {
				
				let data =JSONBig.parse(res.data) ;
				if(data.code==200){
					resolve(data);
				}else if(data.code==11001){
					uni.showToast({
						icon:'none',
						title:'登陆过期，请重新登陆'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/user/login'
						})
					},1000)
				}else{
					// uni.showToast({
					// 	icon:'none',
					// 	title:data.msg
					// })
				}
				
				

			},
			fail(res) {
				uni.showToast({
					icon:'none',
					title:data.code.msg
				})
				// 失败处理
				reject(res);
			}
		});
	});
}

export function server(options) {
	return new Promise((resolve, reject) => {
		
		uni.request({
			url: baseUrl + options.url,
			data: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"Authorization": uni.getStorageSync('userInfo').accessToken || "",
				"Content-Type": "application/json",
				"ADMIN_ID": adminId,
				"USER_SERVICE_TOKEN":userToken,
				"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
			},
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				if(data.code==200){
					resolve(data);
				}else if(data.code==11001){
					uni.showToast({
						icon:'none',
						title:'登陆过期，请重新登陆'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/user/login'
						})
					},1000)
				}else{
					// uni.showToast({
					// 	icon:'none',
					// 	title:data.msg
					// })
				}

			},
			fail(res) {
				// 失败处理
				reject(res);
			}
		});
	});
}


export function serverUpload(options) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl + options.url,
			formData: options.data || {},
			method: options.method,
			header: {
				"testToken": "2gq72h2qrbhx256y0167uf5wd64ls55u",
				"BGDEBUG": 0,
				"Authorization": uni.getStorageSync('userInfo').accessToken || "",
				"Content-Type": "application/json",
				"ADMIN_ID": adminId,
				"USER_SERVICE_TOKEN":userToken,
				"clientId": uni.getStorageSync('__DC_STAT_UUID') || "",
			},
			filePath: options.data.file,
			name: 'file',
			dataType: 'string',
			success(res) {
				let data =JSONBig.parse(res.data) ;
				if(data.code==200){
					resolve(data);
				}else if(data.code==11001){
					uni.showToast({
						icon:'none',
						title:'登陆过期，请重新登陆'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/user/login'
						})
					},1000)
				}else{
					uni.showToast({
						icon:'none',
						title:data.msg
					})
				}
			},
			fail(res) {
				// 失败处理
				reject(res);
			}
		});
	});
}



